package org.sny7.openOfflineList.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;

import org.sny7.openOfflineList.app.ProgramExit;
import org.sny7.openOfflineList.listener.LanguageMenuClick;
import org.sny7.openOfflineList.listener.SkinMenuClick;
import org.sny7.openOfflineList.util.FrameKeeper;
import org.sny7.openOfflineList.util.LanguageChanger;
import org.sny7.openOfflineList.util.XMLDataManager;

public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	
	private static MainFrame mainFrame = null;

	private MainFrame() {
		initGUI();
	}

	private void initGUI() {

		LanguageChanger.localeThis(this, "mainFrame-Title");

		this.getContentPane().setLayout(new BorderLayout());

		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent event) {
				ProgramExit.go();
			}
			public void windowOpened(WindowEvent e) {
				Splash.setLocationRelativeTo2(mainFrame);
				XMLDataManager.loadFile("datas/ADVANsCEne_NDS_S.zip");
			}
		});
		
		this.setSize(800, 600);

		JMenuBar menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);

		JMenu fileMenu = new JMenu();
		LanguageChanger.localeThis(fileMenu, "fileMenu-Text");
		fileMenu.setMnemonic(KeyEvent.VK_F);
		menuBar.add(fileMenu);

		JMenuItem exitMenuItem = new JMenuItem();
		LanguageChanger.localeThis(exitMenuItem, "exitMenuItem-Text");
		exitMenuItem.setMnemonic(KeyEvent.VK_E);
		exitMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ProgramExit.go();
			}
		});
		fileMenu.add(exitMenuItem);
		
		JMenu optionMenu = new JMenu();
		LanguageChanger.localeThis(optionMenu, "optionMenu-Text");
		optionMenu.setMnemonic(KeyEvent.VK_O);
		menuBar.add(optionMenu);

		JMenu skinMenu = new JMenu();
		LanguageChanger.localeThis(skinMenu, "skinMenu-Text");
		skinMenu.setMnemonic(KeyEvent.VK_S);
		skinMenu.addMenuListener(new SkinMenuClick());
		menuBar.add(skinMenu);
		
		JMenu languageMenu = new JMenu();
		languageMenu.setText("Language");
		languageMenu.setMnemonic(KeyEvent.VK_L);
		languageMenu.addMenuListener(new LanguageMenuClick());
		menuBar.add(languageMenu);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
		splitPane.setResizeWeight(1);
		this.add(splitPane);

		JPanel westPanel = new JPanel();
		westPanel.setLayout(new BorderLayout());
		splitPane.setLeftComponent(westPanel);
		
		GameListSearcher gameListSearch = GameListSearcher.getGameListSearcher();
		westPanel.add(gameListSearch, BorderLayout.NORTH);
		
		JScrollPane gameListScrollPane = new JScrollPane();
		
		GameList gameList = GameList.getGameList();
		gameListScrollPane.getViewport().add(gameList);
		westPanel.add(gameListScrollPane, BorderLayout.CENTER);
		
		JSplitPane eastPanel = new JSplitPane();
		eastPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
		eastPanel.setResizeWeight(1);
		eastPanel.setEnabled(false);
//		eastPanel.setOneTouchExpandable(true);
		splitPane.setRightComponent(eastPanel);
		
		JPanel infoPanel = new JPanel();
		eastPanel.setLeftComponent(infoPanel);
		
		FilterPanel filterPanel = FilterPanel.getFilterPanel();
		eastPanel.setRightComponent(filterPanel);
	}

	public static void display() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				mainFrame = new MainFrame();
				FrameKeeper.addFrame(mainFrame);
				mainFrame.setLocationRelativeTo(null);
				mainFrame.setVisible(true);
			}
		});
	}
	
	public static void setEnabled2(boolean b) {
		mainFrame.setEnabled(b);
	}

}
